package com.woniuxy.microgatewayserver.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * Auther: wuson  <br/>
 * Date: 2021/3/11:12:17  <br/>
 * Description:
 */
@Configuration
public class RedisLimitConFig {
    /**
     * 根据接口路径限流
     * @Primary 优先选择（必须加此注解）
     * @return
     */
    @Primary
    @Bean
    public KeyResolver pathKeyResolver(){
        return exchange ->
                Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
    /**
     * 根据IP地址来进行限流设置
     * @return
     */
    @Bean
    public KeyResolver ipKeyResolver(){
        return exchange ->
                Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
    /**
     * 根据字段限流，下文根据Query参数值作为限流标识，请酌情更改为自己的方式
     * @return
     */
    @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }
}
